#include "LinkList.h"

LinkList::LinkList():size(0)
{
    head = new Node(0);
}

LinkList::LinkList(const LinkList &list)
{
    size =list.size;
    head = new Node(0);
    Node *tp = head;
    Node *travelPoint = list.head->next;
    while(travelPoint != nullptr)
    {
        Node *newNode = new Node(travelPoint->value);
        tp->next = newNode;
        tp = tp->next;
        travelPoint = travelPoint->next;
    }
}

LinkList &LinkList::operator=(const LinkList &list)
{
    while(head != nullptr)
    {
        Node *nextNode = head->next;//下一个节点
        delete head;
        head = nextNode;
    }
    size =list.size;
    head = new Node(0);
    Node *tp = head;
    Node *travelPoint = list.head->next;
    while(travelPoint != nullptr)
    {
        Node *newNode = new Node(travelPoint->value);
        tp->next = newNode;
        tp = tp->next;
        travelPoint = travelPoint->next;
    }
    return *this;
}

LinkList::~LinkList()
{
    while(head != nullptr)
    {
        Node *nextNode = head->next;//下一个节点
        delete head;
        head = nextNode;
    }
}

void LinkList::InsertTail(const ElementType &element) //O(n)
{
    Node *travelPoint = head;
    while(travelPoint->next != nullptr)//还没到尾部
        travelPoint = travelPoint->next;
    travelPoint->next = new Node(element);//创建一个相等节点
    size++;
}

void LinkList::InsertHead(const ElementType &element) //O(1)
{
    Node *newNode = new Node(element);
    newNode->next = head->next;
    head->next = newNode;
    size++;
}

void LinkList::InsertByIndex(size_t index, const ElementType &element)
{
    if(index > size)
    {
        std::cout<<"位置非法\n";
        return;
    }
    Node *travelPoint = head;
    for(size_t i = 0;i < index ;i++)
    {
        travelPoint = travelPoint->next;
    }
    Node *newNode = new Node(element);
    newNode->next = travelPoint->next;
    travelPoint->next = newNode;
    size++;
}

void LinkList::RemoveByIndex(size_t index)
{
    if(index >= size)
    {
        std::cout<<"删除位置非法\n";
        return;
    }
    Node *travelPoint = head;
    for(size_t i = 0;i < index;i++)
        travelPoint = travelPoint->next;
    Node *freeNode = travelPoint->next;
    travelPoint->next = freeNode->next;
    delete freeNode;
    size--;
}

void LinkList::RemoveByElemnt(const ElementType &element)
{
    Node *travelPoint = head;
    while(travelPoint->next != nullptr)
    {
        if(travelPoint->next->value == element)
        {
            Node *freeNode = travelPoint->next;
            travelPoint->next = freeNode->next;
            delete freeNode;
            size--;
        }
        else
            travelPoint = travelPoint->next;
    }

}

ElementType *LinkList::FindByIndex(size_t index)
{
    if(index >= size)
        return nullptr;
    Node *travelPoint = head->next;
    for(size_t i = 0; i < index; i++)
        travelPoint = travelPoint->next;
    return &travelPoint->value;
    
}

DARRAY::DynamicArray LinkList::FindByElement(const ElementType &element)
{
    DARRAY::DynamicArray array;
    Node *travelPoint = head->next;
    while(travelPoint != nullptr)
    {
        if(travelPoint->value == element)
        {
            array.InsertTail(&travelPoint->value);
        }
        travelPoint = travelPoint->next;
    }
    return array;
             
}

void LinkList::UpdateByIndex(size_t index, const ElementType &element)
{
    if(index >= size)
    {
        std::cout<<"删除位置非法\n";
        return;
    }
    Node *travelPoint = head->next;
    for(size_t i = 0;i < index;i++)
        travelPoint = travelPoint->next;
    travelPoint->value = element;
}

void LinkList::UpdateByElement(const ElementType &oldvalue, const ElementType &newvalue)
{
    Node *travelPoint = head->next;
    while(travelPoint != nullptr)
    {
        if(travelPoint->value == oldvalue)
        {
           travelPoint->value = newvalue;
        }
        travelPoint = travelPoint->next;
    }
}

void LinkList::BuubleSort()
{
    for(size_t i = 0; i < size - 1 ;i++)
    {
        Node *travelPoint = head; //每次循环回到链表开头
        for(size_t j = 0;j < size - 1 - i;j++)
        {
            if(travelPoint->next->value > travelPoint->next->next->value)
            {
                Node *prev = travelPoint->next;
                Node *back = travelPoint->next->next;
                prev->next = back->next;
                back->next = prev;
                travelPoint->next = back;
            }
        }
    }
}

void LinkList::Sort()
{
    FastSort(head->next,nullptr);
}

void LinkList::FastSort(Node *start, Node *end)
{
    //终止条件
    if(start == end)
        return;
    Node *temp = start;//基准值指针
    Node *slow = start;//慢指针
    Node *fast = start->next;
    while(fast != end)
    {
        //快指针指向的值比基准值小
        if(fast->value < temp->value)
        {
            slow = slow->next;
            std::swap(slow->value,fast->value);
            //slow = slow->next;
        }
        fast = fast->next;
    }
    std::swap(temp->value,slow->value);//将基准值归位
    FastSort(start,slow);//[start,slow)
    FastSort(slow->next,end);
}



ElementType *LinkList::GetReserveIndex(size_t index)
{
    if(index > size || index == 0)
    {
        return nullptr;
    }
    Node *travelPoint = head->next;
    for(size_t i = 0; i < size - index; i++)
        travelPoint = travelPoint->next;
    return &travelPoint->value;
}

LinkList LinkList::Merge(const LinkList &list)
{
    LinkList result;
    Node* first = head->next;
    Node* second = list.head->next;
    while(first  && second )
    {
        if(first->value < second->value)
        {
            result.InsertTail(first->value);
            first = first->next;
        }
        else 
        {
            result.InsertTail(second->value);
            second = second->next;
        }
    }
    while(first)
    {
        result.InsertTail(first->value);
        first = first->next;
    }
    while(second)
    {
        result.InsertTail(second->value);
        second = second->next;
    }
    return result;

}

LinkList LinkList::FdIntrSctn(const LinkList &list)
{
    LinkList result;
    Node* first = head->next;
    Node* second = list.head->next;
    while(first  && second )
    {
        if(first->value == second->value)
        {
            result.InsertTail(first->value);
            first = first->next;
            second = second->next;
        }
        else if(first->value < second->value)
            first = first->next;
        else if(first->value > second->value)
            second = second->next;
    }
    return result;

}

LinkList LinkList::FindUnionSet(const LinkList &list)
{
    LinkList result;
    Node* first = head->next;
    Node* second = list.head->next;
    while(first  && second )
    {
        if(first->value == second->value)
        {
            result.InsertTail(first->value);
            first = first->next;
            second = second->next;
        }
        else if(first->value < second->value)
        {
            result.InsertTail(first->value);
            first = first->next;
        }
        else if(first->value > second->value)
        {
            result.InsertTail(second->value);
            second = second->next;
        }        
    }
    while(first)
    {
        result.InsertTail(first->value);
        first = first->next;
    }
    while(second)
    {
        result.InsertTail(second->value);
        second = second->next;
    }
    return result;
}

LinkList LinkList::FindDifference(const LinkList &list)
{
        LinkList result;
    Node* first = head->next;
    Node* second = list.head->next;
    while(first  && second )
    {
        if(first->value == second->value)
        {
            first = first->next;
            second = second->next;
        }
        else if(first->value < second->value)
        {
            result.InsertTail(first->value);
            first = first->next;
        }
        else if(first->value > second->value)
        {
            result.InsertTail(second->value);
            second = second->next;
        }        
    }
    while(first)
    {
        result.InsertTail(first->value);
        first = first->next;
    }
    while(second)
    {
        result.InsertTail(second->value);
        second = second->next;
    }
    return result;
}

void LinkList::Reserve()
{
    Node *prev = nullptr;
    Node *cur = head->next;
    Node *Next = cur->next;
    while(Next != nullptr)//一次只交换一个指针
    {
        cur->next = prev;
        prev = cur;
        cur = Next;
        Next = cur->next;
    }
    cur->next = prev; //最后一个节点单独逆序
    head->next = cur;
}

void LinkList::Reserve2()
{
    head->next = reserve(head->next);
}

LinkList::Node *LinkList::reserve(Node *node)
{
    //返回值是原来的尾吧，新链表的头
    if(node->next == nullptr)//尾部 逆序完的头
        return node;
    else
    {
        Node *newHead = reserve(node->next);//递归往后走
        node->next->next = node;
        node->next = nullptr;//确保新尾巴 指向空
        return newHead;
    }
}

void LinkList::show()
{
    std::cout<<"size : "<<size<<std::endl;
    Node *travelPoint = head->next;
    while(travelPoint != nullptr)//链表还没有结束
    {
        //std::cout<<travelPoint->next<<std::endl;
        std::cout<<travelPoint->value<<" ";
        travelPoint = travelPoint->next;//遍历指针往后走
    }
    std::cout<<std::endl;    
}

